package com.titaniume.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ProjectName: juc
 * @Package: com.titaniume.lock
 * @ClassName: ThreadDemo3
 * @Author: huangrenfei
 * @Description: 线程间定制化通信
 *
 * 启动三个线程 按照下面要求
 * AA 打印5次 BB打印10次 CC打印15次
 * 进行10轮
 *
 * flag  = 1 AA执行打印5次  修改标志位flag = 2 通知BB
 * flag  = 2 BB执行打印10次 修改标志位flag = 3 通知CC
 * flag  = 3 CC执行打印15次 修改标志位flag = 1 通知AA
 *
 * @Date: 2021/11/22 15:13
 * @Version: 1.0
 */
//第一步 创建资源类

class ShareResource {
    //定义标志位
    private int flag = 1;  //1 AA  2 BB   3 CC

    //创建Lock锁
    private Lock lock = new ReentrantLock();

    //创建三个Condition
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();


    /***
     * @description: 打印5次
     * @param loop 第几轮
     * @author: renfei.huang
     * @date: 2021/11/22  15:18
     */
    public void print5(int loop) throws Exception {
        //上锁
        lock.lock();
        try {
            //判断
            if (flag != 1) {
                //等待
                c1.await();
            }
            //干活
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName() + " ::" + i + ",轮数:" + loop);
            }
            //通知
            flag = 2;  //先修标志位 2
            c2.signalAll(); //在通知BB线程

        } finally {
            //释放锁
            lock.unlock();
        }
    }


    /***
     * @description: 打印10次
     * @param loop 轮数
     * @author: renfei.huang
     * @date: 2021/11/22  15:26
     */
    public void print10(int loop) throws Exception {
        lock.lock();
        try {
            while (flag != 2) {
                c2.await();
            }
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName() + " ::" + i + ", 轮数:" + loop);
            }
            //修改标志位
            flag = 3;
            //通知CC线程
            c3.signalAll();

        } finally {
            lock.unlock();
        }
    }


    /***
     * @description: 打印15次
     * @param loop 轮数
     * @author: renfei.huang
     * @date: 2021/11/22  15:27
     */
    public void print15(int loop) throws Exception {
        lock.lock();
        try {
            while (flag != 3) {
                c3.await();
            }
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName() + " ::" + i + ", 轮数:" + loop);
            }
            //修改标志位
            flag = 1;
            //通知CC线程
            c1.signalAll();
        } finally {
            lock.unlock();
        }
    }

}

public class ThreadDemo3 {


    public static void main(String[] args) {

        ShareResource shareResource = new ShareResource();

        new Thread( () ->{
            for (int i = 1; i <= 10; i++) {
                try {
                    shareResource.print5(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"AA").start();

        new Thread( () ->{
            for (int i = 1; i <= 10; i++) {
                try {
                    shareResource.print10(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"BB").start();

        new Thread( () ->{
            for (int i = 1; i <= 10; i++) {
                try {
                    shareResource.print15(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },"CC").start();
    }
}
